//
//  SHPictureViewController.m
//  UI_CarHome
//
//  Created by dllo on 15/11/3.
//  Copyright (c) 2015年 沈浩. All rights reserved.
//

#import "SHPictureViewController.h"
#import "SHHandle.h"
#import "SHPicCollectionViewCell.h"
#import "SHMorePicController.h"
#import "SHCollectionReusableView.h"
@interface SHPictureViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property(nonatomic, retain)UICollectionView *collectionView;
@property(nonatomic, retain)NSMutableArray *sectionsArr;
@end

@implementation SHPictureViewController
- (void)dealloc
{
    [_collectionView release];
    [_nextStr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = BACKGROUNDCOLOR;
    self.title = @"图片";
    [self createData];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(isNight:) name:@"isNight" object:nil];
    self.collectionView.backgroundColor = BACKGROUNDCOLOR;
    
}
- (void)isNight:(NSNotification *)nsnotification {
    self.collectionView.backgroundColor = BACKGROUNDCOLOR;
    
    [self.collectionView reloadData];
}

- (void)createData {
    [SHNetWorkingTool getNetWorking:self.nextStr block:^(id result) {
        self.sectionsArr = [NSMutableArray array];
        self.sectionsArr = result[@"sections"];
        [self.collectionView reloadData];
    }];
    [self.view addSubview:self.collectionView];
    [_collectionView release];
}
#pragma mark --collectionView
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.itemSize = CGSizeMake(100, 70);
        flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:flowLayout];
        self.collectionView.dataSource = self;
        self.collectionView.delegate = self;
        self.collectionView.backgroundColor = [UIColor whiteColor];
        [self.collectionView registerClass:[SHPicCollectionViewCell class] forCellWithReuseIdentifier:@"collection"];
        [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"more"];
        [self.collectionView registerClass:[SHCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
        flowLayout.headerReferenceSize = CGSizeMake(80, 30);
    }
    return _collectionView;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
   
        
        NSDictionary *tempDic = self.sectionsArr[indexPath.section];
        NSArray *photosArr = tempDic[@"photos"];
        NSNumber *totalNum = tempDic[@"total"];
    if (photosArr.count > indexPath.row) {

            SHPicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collection" forIndexPath:indexPath];
            cell.contentView.backgroundColor = [UIColor cyanColor];
            cell.photosDic = tempDic[@"photos"][indexPath.row];
            return cell;
        }
        if ([totalNum intValue] > 5 && indexPath.row == 5){
            UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"more" forIndexPath:indexPath];
            cell.contentView.backgroundColor = [UIColor lightGrayColor];
            UILabel *moreLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 70)];
            [cell.contentView addSubview:moreLabel];
            [moreLabel release];
            moreLabel.text = @"查看更多>";
            moreLabel.textColor = NEWBLUE;
            moreLabel.textAlignment = NSTextAlignmentCenter;
            return cell;
        }
    return nil;
   }
#pragma mark --collectionView 点击
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 5) {
        SHMorePicController *morePic = [[SHMorePicController alloc] init];
        [self.navigationController pushViewController:morePic animated:YES];
        [morePic release];
        morePic.myId = self.carId;
        morePic.typeId = indexPath.section + 1;
        NSDictionary *dic = self.sectionsArr[indexPath.section];
        morePic.typeTitle = dic[@"title"];
    }
}

#pragma mark --collectionView 当前分区多少行
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSDictionary *tempDic = self.sectionsArr[section];
    NSNumber *totalNum = tempDic[@"total"];
    if ([totalNum intValue] > 5 && [tempDic[@"photos"] count] == 5) {
        return 6;
    } else {
        return [self.sectionsArr[section][@"photos"] count];
    }
}
#pragma mark --collectionView 多少分区
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return self.sectionsArr.count;
}

#pragma mark --collectionView 分区标题
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {
        SHCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerView" forIndexPath:indexPath];
        NSDictionary *headerDic = self.sectionsArr[indexPath.section];
        headerView.label.text = [NSString stringWithFormat:@" %@(%@张)", headerDic[@"title"],headerDic[@"total"]];
        return headerView;
    }
    return nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
//     Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
